/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef CHIP_ADS78_H
#define CHIP_ADS78_H

#include "internal/bus/bus_base/bus.h"
#include "internal/chip/chip_base/chip.h"
#include <mc/engine.h>
#include <memory>

namespace dev {

class MC_API chip_ads78 : public chip_base {
private:
    // ADS78 Register Constants
    static constexpr uint32_t MAX_OFFSET_INDEX    = 7;
    static constexpr uint32_t BYTES_PER_REGISTER  = 2;
    static constexpr uint32_t BITS_PER_BYTE       = 8;
    static constexpr uint32_t REGISTER_CMD_MASK   = 0x4;
    static constexpr uint32_t REGISTER_SHIFT_BITS = 4;

    // ADS78 ADC Conversion Constants
    static constexpr uint32_t ADC_REFERENCE_VOLTAGE_MV = 2500; // Reference voltage in millivolts
    static constexpr uint32_t ADC_MAX_VALUE            = 4096; // 12-bit ADC maximum value (2^12)

    // Helper methods
    static uint32_t get_register_address(uint32_t channel_offset);
    static uint16_t convert_adc_to_millivolts(uint16_t adc_value);

public:
    MC_API chip_ads78()  = default;
    MC_API ~chip_ads78() = default;

    MC_API bool init(mc::mutable_dict& csr_object);

    // Override base class methods
    MC_API int32_t bit_read(uint32_t offset, uint8_t length, uint32_t mask,
                             std::vector<uint8_t>& in_buffer,
                             std::vector<uint8_t>& out_buffer, std::string& error) override;
};

} // namespace dev

#endif // CHIP_ADS78_H
